package main

import "fmt"

//内存和用时均超过87%
func numIslands(grid [][]byte) int {
	num := 0
	for i := 0; i < len(grid); i++ {
		for j := 0; j < len(grid[0]); j++ {
			if grid[i][j] == 49 {
				num++
				dfs(grid, i, j)
			}
		}
	}
	return num
}

func isArea(grid [][]byte, r, c int) bool {
	return r >= 0 && r < len(grid) && c >= 0 && c < len(grid[0])
}

func dfs(grid [][]byte, r, c int) {
	if !isArea(grid, r, c) {
		return
	}
	if grid[r][c] != 49 {
		return
	} else {
		grid[r][c] = 50
	}
	dfs(grid, r+1, c)
	dfs(grid, r-1, c)
	dfs(grid, r, c+1)
	dfs(grid, r, c-1)

}

func main() {
	c := '2'
	fmt.Println(c)
	grid := [][]byte{{48, 49}, {49, 48}}
	// for i := 0; i < len(grid); i++ {
	// 	for j := 0; j < len(grid[0]); j++ {
	// 		fmt.Printf("%c\n", grid[i][j])
	// 	}
	// }
	// fmt.Println(grid)
	fmt.Println(numIslands(grid))
}
